package net.ocps.tchs.soap.object;

import net.ocps.tchs.soap.Main;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;

public class Platform extends StaticObject {

	public Platform(int x, int y, int width, int height) {
		super(x, y);
		this.width = width;
		this.height = height;
	}

	public Platform(int x, int y, int width, int height, String imagePath) {
		super(x, y);
		this.width = width;
		this.height = height;
		this.imagePath = imagePath;
	}

	@Override
	public void render() {
		try {
			batch.begin();
			{
				x = groundBody.getPosition().x-(width/2);
				y = groundBody.getPosition().y-(height/2);

				batch.setProjectionMatrix( Main.camera.combined );
				batch.draw(
						texture, x, y, texture.getOriginX(), texture.getOriginY(), 
						width, height, texture.getScaleX(), 
						texture.getScaleY(), 0f );
				//batch.draw(texture, x, y, width, height);
			}
			batch.end();
		} catch( Exception e ) {
			if(batch != null)
				batch.end();
		}
	}

	@Override
	public void onCreate() {
		batch = new SpriteBatch();
		texture = new Sprite( new Texture(Gdx.files.internal(imagePath)) );
		BodyDef groundBodyDef = new BodyDef();  
		groundBodyDef.position.set(new Vector2( x+(width/2), y-(height/2) ));  
		groundBody = Main.world.createBody(groundBodyDef);  
		PolygonShape groundBox = new PolygonShape();  
		groundBox.setAsBox(width/2, height/2);  
		groundBody.createFixture(groundBox, 0.0f);
	}

	public int width, height;
	public SpriteBatch batch;
	public Sprite texture;
	public Body groundBody;
}
